var Conf = function(app) {
    this.app = app;
};


// 配置pomelo-globalchannel-plugin
Conf.prototype.globalChannel = function() {
    var globalChannel = require('pomelo-globalchannel-plugin');
    this.app.use(globalChannel, {globalChannel: {
        host: '127.0.0.1',
        port: '6379'
    }});
}



// 配置pomelo-status-plugin
Conf.prototype.statusChannel = function() {
    var status = require('pomelo-status-plugin');
    this.app.use(status, {status: {
        host: '127.0.0.1',
        port: 6379
    }});
};


var common = require('./app/util/common');
var sceneManager = require('./app/util/sceneManager');

// 配置路由
// routeParam为session或者场景ID
Conf.prototype.configureRoute = function() {
    var sceneRoute = function(routeParam, msg, app, cb) {
        var sceneServers = app.getServersByType('scene');

        if (!sceneServers || sceneServers.length === 0) {
            cb(new Error('can not find scene servers.'));
            return;
        }

        var scene = 0;
        if (isNaN(routeParam)) {
            if (isNaN(routeParam.get('scene'))) {
                scene = common.getRandomNum(0, sceneServers.length - 1);
            } else {
                scene = routeParam.get('scene');
            }
        } else {
            scene = routeParam;
        }
        sceneManager.getServerIdByScene(app, scene, function(err, id) {
            if (err) {
                cb(err);
            } else {
                cb(null, id);
            }
        });
    };
    this.app.route('scene', sceneRoute);
};


// 配置缓存数据库redis
var cache = require('./app/database/cache');
Conf.prototype.initCacheDB = function() {
    var cachedb = cache('127.0.0.1', 6379);
    this.app.set('cachedb', cachedb);
};

// 配置mysql数据库
var mysql = require('./app/database/db');
Conf.prototype.initDB = function() {
    var db = mysql();
    this.app.set('db', db);
};

module.exports = Conf;
